#Region " Imports "

Imports BlazeApps.Library.Page
Imports System.Web.UI
Imports System.Data

#End Region

Partial Class Usercontrols_Pagelinks
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Protected FTBID As String
    Private DataPageLocal As New PagedDataSource

    Public Property sortCriteria() As String
        Get
            Return CStr(ViewState("sortCriteria"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortCriteria") = Value
        End Set
    End Property

    Public Property sortDir() As String
        Get
            Return CStr(ViewState("sortDir"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortDir") = Value
        End Set
    End Property

    Public Property DataLoaded() As Boolean
        Get
            Return GetBooleanFromVS("DataLoaded")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("DataLoaded", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("Page Links")

        FTBID = GetStringFromQueryString("ftbid")

        If Me.Page.IsPostBack = False Then

            'Set Default Sort Order and Direction
            Me.sortCriteria = "title"
            Me.sortDir = "asc"

            LoadPage()
        End If

        ConfigBaseNavCtrls()

        'If Me.Page.IsPostBack = False Then
        '    BindDataItems()
        'End If

    End Sub

#End Region

#Region " Save Data "



#End Region

#Region " Load Data "

    Private Sub LoadPage()
        LoadPageSize(Me.uxPageSizeDropDownList)
    End Sub

    Public Overloads Overrides Sub BindDataItems()

        Dim List As New PageList
        Dim DS As New DataSet
        Dim DT As New DataTable
        Dim DV As New DataView

        DS = List.GetPageList(ItemTypeList.Page, StatusList.Enabled, Me.uxStartDateTextBox.Text.ToString.Trim, Me.uxEndDateTextBox.Text.ToString.Trim, Me.uxSearchTextBox.Text.ToString.Trim)

        Me.RecordCount = DS.Tables(0).Rows.Count

        DT = DS.Tables(0)

        DV.Table = DT

        DV.Sort = Me.sortCriteria & " " & Me.sortDir

        Me.DataPage.DataSource = DV
        Me.DataPage.AllowPaging = True
        Me.DataPage.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.DataPage.CurrentPageIndex = Me.PageNumber

        CreatePagingLinks()

        DataLoaded = True

        With Me.uxResultsGridView
            .DataSource = Me.DataPage
            .DataBind()
        End With

        DataLoaded = False

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxSubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSubmitButton.Click
        BindDataItems()
    End Sub

    Protected Sub uxResetButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxResetButton.Click

    End Sub

    Protected Sub uxSaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveButton.Click
        SendLinkValue()
    End Sub

    Protected Sub uxResultsGridView_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles uxResultsGridView.RowCreated

        If DataLoaded = True Then
            If e.Row.RowType = DataControlRowType.DataRow Then
                e.Row.Visible = ShowItem(ConvertToGuid(e.Row.DataItem("pageid").ToString))
            End If
        End If

    End Sub

    Protected Sub uxResultsGridView_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles uxResultsGridView.Sorting
        'Get the Sort by Value from the Sortexpression field on the column
        If Me.sortCriteria = e.SortExpression Then
            If Me.sortDir = "desc" Then
                Me.sortDir = "asc"
            Else
                Me.sortDir = "desc"
            End If
        End If

        ' Assign the column clicked to the sortCriteria property
        Me.sortCriteria = e.SortExpression

        'Calls the function to rebuild the datagrid view, with the new sort order
        BindDataItems()
    End Sub

#End Region

#Region " Helpers and Methods "

    Private Sub ConfigBaseNavCtrls()

        Me.PagingTopPanel = Me.uxPagingTopPanel
        Me.PagingBottomPanel = Me.uxPagingBottomPanel

        Me.RepeaterTop = Me.uxPagesTopRepeater
        Me.RepeaterBottom = Me.uxPagesBottomRepeater

        Me.NextTopLinkButton = Me.uxTopNextLinkButton
        Me.PreviousTopLinkButton = Me.uxTopPrevLinkButton
        Me.NextBottomLinkButton = Me.uxBottomNextLinkButton
        Me.PreviousBottomLinkButton = Me.uxBottomPrevLinkButton

        Me.DataPage = DataPageLocal
        Me.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.PagesToDisplay = 10

        Me.ItemsPerPageDropDown = Me.uxPageSizeDropDownList
        Me.ItemsPerPageLabel = Me.uxItemsPerPageLabel
        Me.PagerInfoLabel = Me.uxPagerInfoLabel

        AddHandler Me.RepeaterTop.ItemCommand, AddressOf PagingRepeater_ItemCommand
        AddHandler Me.RepeaterBottom.ItemCommand, AddressOf PagingRepeater_ItemCommand

    End Sub

    Private Sub SendLinkValue()

        Dim List As New StringBuilder
        Dim iI As Integer = 0

        While iI < Me.uxResultsGridView.Rows.Count
            Dim Row As GridViewRow = Me.uxResultsGridView.Rows(iI)
            Dim Link As String = CType(Row.FindControl("uxViewerHyperLink"), HyperLink).NavigateUrl.ToString
            Dim Title As String = CType(Row.FindControl("uxPageDescTextBox"), TextBox).Text.ToString.Trim
            Dim Checked As Boolean = CType(Row.FindControl("uxSelectCheckBox"), CheckBox).Checked

            If Checked Then
                List.Append(vbCrLf)
				'List.Append("window.opener.FTB_API[""" & FTBID & """].InsertHtml(""<a href='" & Replace(Link, "~/", "") & "' target='_blank'>" & Title & "</a><br>"");")
				List.Append(String.Format("opener.document.getElementById('txtUrl').value = ""<a href='{0}{1}' target='_blank'>{2}</a><br>"");", AppPath, Replace(Link, "~/", ""), Title))
                List.Append(vbCrLf)
            End If

            System.Math.Min(System.Threading.Interlocked.Increment(iI), iI - 1)

        End While

        List.Append("window.close();")
        List.Append(vbCrLf)

        List.Append(vbCrLf)

        ScriptManager.RegisterStartupScript(uxSaveButton, Me.GetType, "SendPageLinkValues", List.ToString, True)

    End Sub

#End Region

End Class
